import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../../../providers/ApiPro.service';
import { parts } from '../HuoSettmp.model';
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from '@angular/forms';
import { NzModalService } from 'ng-zorro-antd';


@Component({
  selector: 'app-SetAdmin',
  templateUrl: './SetAdmin.component.html',
  styleUrls: ['./SetAdmin.component.scss']
})
export class SetAdminComponent implements OnInit {
  data = [];
  public _total = 0;
  public pageIndex = 1;
  public parts: parts = new parts();
  public isVisible = false;
  public validateForm: FormGroup;
  public isConfirmLoading = false;
  public isError: boolean = false;
  public isEdit: boolean;



  constructor(
    public apiService: ApiService,
    private fb: FormBuilder,
    private confirmServ: NzModalService

  ) { }

  ngOnInit() {
    this.loadAll();
    this.initForm();
  }

    loadAll(){
    let url = "findpositions";
    if(!this.parts.positionName){
      delete this.parts.positionName;
    }
    this.apiService.get(url,this.parts)
      .then(res =>{
        this._total = res.totalCount;
        this.data = res.result;
      })
  }

   initForm(){
    this.validateForm = this.fb.group({
      isEnable: [ null, [ Validators.required ] ],
      positionId: [ null,],
      positionName: [ null, [ Validators.required ] ],
    });
  }


  resetIndex(e) {
        this.parts.setPage(e);
        this.loadAll();
  }

  open(){
    this.isVisible = true;
    this.initForm();
  }

  editer(e){
    let url = "findpositiondetail";
    this.isEdit = true;
    this.apiService.get(url,{"id":e})
      .then( res =>{
        this.validateForm = this.fb.group({
          isEnable: [ res.result.isEnable , [ Validators.required ] ],
          positionId: [ res.result.positionId,],
          positionName: [ res.result.positionName, [ Validators.required ] ],
        });
        this.isVisible = true;
      })
  }

  showModal = () => {
    this.isVisible = true;
  }

  handleCancel = (e) => {
    this.isVisible = false;
  }

  handleOk () {
    this.isConfirmLoading = true;
    this._submitForm();
    if(this.isError){
        setTimeout(() => {
          this.isConfirmLoading = false;
          this.isError = false;
        }, 1500);
    }else{
       setTimeout(() => {
          this.isVisible = false;
          this.isConfirmLoading = false;
        }, 300);
    }
  }

  getFormControl(name) {
    return this.validateForm.controls[ name ];
  }

  _submitForm() {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
    }
    if(this.validateForm.valid){
      this.createNew();
    }else{
      this.isError = true;
    }
  }

  createNew(){
    let url = "addposition";
    let urls = "editposition";
    if(this.isEdit){
        this.apiService.post(urls,this.validateForm.value)
        .then( res =>{
          if(res.result){
            this.loadAll();
          }else{
            this.error(res.errMsg);
          }
      })
    }else{
          this.apiService.post(url,this.validateForm.value)
        .then( res =>{
          if(res.result){
            this.loadAll();
          }else{
            this.error(res.errMsg);
          }
      })
    }
}

  error(err) {
    this.confirmServ.error({
      title: '错误',
      content: err,
      okText:'返回'
    });
  }

}
